home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C++ / Snippets / AppLauncher 1.0d0 / _(original postings) / UAEFinder.p (peter@kagi.com) < prev   
Encoding:
Text File  |  1995-03-14  |  12.7 KB  |  383 lines  |  [TEXT/CWIE]

  1.  
  2. In article <reter-1402950852190001@bedrock.med.unc.edu>, reter@med.unc.edu
  3. (Roy W. Reter) wrote:
  4.  
  5. >I need some help.  I am writing an app that I am getting the passed the
  6. >path information of an application I need to launch in a string and need
  7. >then to launch that application and terminate the one that read in the
  8. >file.  Is there a way to do this (I am writing this in CodeWarrior)?
  9.  
  10. This is a generic programming question, you should post it to
  11. comp.sys.mac.programmer.help, not csmp.codewarrior just because you are
  12. using CodeWarrior.  I'm posting this to those two and alt.sources.mac. 
  13. Followups are redirected to csmph.
  14.  
  15. Here is some Pascal source code for launching applications, amoungst other
  16. things.
  17.  
  18. Enjoy,
  19.    Peter.
  20.  
  21. unit MyProcesses;
  22.  
  23. interface
  24.  
  25.    uses
  26.       Files, Memory, Processes;
  27.  
  28.    const
  29.       application = 'APPL';
  30.  
  31.    function FindApplication (creator: OSType; var fs: FSSpec): OSErr;
  32.    function FindProcess (creator, typ: OSType; var process:ProcessSerialNumber; var fs: FSSpec): boolean;
  33.    procedure LaunchWithDocument (creator, typ: OSType; fs: FSSpec; tofront: boolean);
  34.    procedure LaunchApp (creator, typ: OSType; tofront: boolean);
  35.    procedure QuitApplication (creator, typ: OSType);
  36.    procedure LaunchFSSpec (var fs: FSSpec; tofront: boolean);
  37.    function FindControlPanel (fcreator: OSType; var fs: FSSpec): OSErr;
  38.    function TellFinderToLaunch (fs: FSSpec; tofront: boolean): boolean;
  39.    function OpenControlPanel (fcreator: OSType): boolean;
  40.  
  41. implementation
  42.  
  43.    uses
  44.       AppleEvents, Aliases, MySystemGlobals, MyUtils, MyAEUtils, Folders,
  45. GestaltEqu;
  46.  
  47.  
  48.  
  49.    procedure AddFSSToAEList (var list: AEDescList; row: integer; var fs: FSSpec);
  50.       var
  51.          fileAlias: AliasHandle;
  52.          err: OSErr;
  53.    begin
  54.       err := NewAlias(nil, fs, fileAlias);
  55.       if err = noErr then begin
  56.          HLock(handle(fileAlias));
  57.          err := AEPutPtr(list, row, typeAlias, ptr(fileAlias^), fileAlias^^.aliasSize);
  58.          DisposeHandle(handle(fileAlias));
  59.       end;
  60.    end;
  61.  
  62.  
  63.  
  64.    function FindControlPanel (fcreator: OSType; var fs: FSSpec): OSErr;
  65.       var
  66.          pb: HParamBlockRec;
  67.          filename: Str63;
  68.          i: integer;
  69.          err: OSErr;
  70.          vrn: integer;
  71.          dirID: longInt;
  72.    begin
  73.       err := FindFolder(kOnSystemDisk, kControlPanelFolderType, false, fs.vRefNum, fs.parID);
  74.       if err = noErr then begin
  75.          i := 1;
  76.          repeat
  77.             pb.ioNamePtr := @fs.name;
  78.             pb.ioVRefNum := fs.vRefNum;
  79.             pb.ioDirID := fs.parID;
  80.             pb.ioFDirIndex := i;
  81.             i := i + 1;
  82.             err := PBHGetFInfoSync(@pb);
  83.             if err = noErr then begin
  84.                if (pb.ioFlFndrInfo.fdType = 'cdev') & (pb.ioFlFndrInfo.fdCreator = fcreator) then begin
  85.                   leave;
  86.                end;
  87.             end;
  88.          until (err <> noErr);
  89.       end;
  90.       FindControlPanel := err;
  91.    end;
  92.  
  93.  
  94.  
  95.  
  96.    function TellFinderToLaunch (fs: FSSpec; tofront: boolean): boolean;
  97.       var
  98.          process: ProcessSerialNumber;
  99.          err, junk: OSErr;
  100.          targetAddress: AEDesc;
  101.          fileList: AEDescList;
  102.          theEvent, theReply: AppleEvent;
  103.          sendmode: AESendMode;
  104.          gv: longInt;
  105.          finder_fs: FSSpec;
  106.    begin
  107.       err := -1;
  108.       if (Gestalt(gestaltFinderAttr, gv) = noErr) & BTST(gv,gestaltOSLCompliantFinder) then begin
  109.          if FindProcess('MACS', 'FNDR', process, finder_fs) then begin
  110.             AECreate(theEvent);
  111.             AECreate(theReply);
  112.             AECreate(fileList);
  113.             AECreate(targetAddress);
  114.             err := AECreateDesc(typeProcessSerialNumber, @process, sizeof(process), targetAddress);
  115.             if err = noErr then
  116.                err := AECreateAppleEvent(kCoreEventClass, kAEOpenDocuments, targetAddress, kAutoGenerateReturnID, kAnyTransactionID, theEvent);
  117.             AEDestroy(targetAddress);
  118.             if err = noErr then
  119.                err := AECreateList(nil, 0, false, fileList);
  120.             if err = noErr then
  121.                AddFSSToAEList(fileList, 1, fs);
  122.             if err = noErr then
  123.                err := AEPutParamDesc(theEvent, keyDirectObject, fileList);
  124.             if err = noErr then begin
  125.                sendmode := kAENoReply;
  126.                if not tofront then begin
  127.                   sendmode := sendmode + kAENeverInteract;
  128.                end;
  129.                err := AESend(theEvent, theReply, sendmode, kAEHighPriority, kNoTimeOut, nil, nil);
  130.             end;
  131.             AEDestroy(theEvent);
  132.             AEDestroy(theReply);
  133.             AEDestroy(fileList);
  134.             if (err = noErr) & tofront then begin
  135.                junk := SetFrontProcess(process);
  136.             end;
  137.          end;
  138.       end;
  139.       TellFinderToLaunch := err = noErr;
  140.    end;
  141.  
  142.  
  143.  
  144.    function OpenControlPanel (fcreator: OSType): boolean;
  145.       var
  146.          fs: FSSpec;
  147.    begin
  148.       OpenControlPanel := false;
  149.       if FindControlPanel(fcreator, fs) = noErr then begin
  150.          OpenControlPanel := TellFinderToLaunch(fs, true);
  151.       end;
  152.    end;
  153.  
  154.  
  155.  
  156.    function FindApplication (creator: OSType; var fs: FSSpec): OSErr;
  157.       var
  158.          i: integer;
  159.          pbdt: DTPBRec;
  160.          crdate: longInt;
  161.          oe: OSErr;
  162.          found: boolean;
  163.    begin
  164.       found := false;
  165.       if system7 then begin
  166.          i := 1;
  167.          repeat
  168.             fs.vRefNum := 0;
  169.             oe := GetVolInfo(fs.name, fs.vRefNum, i, crdate);
  170.             i := i + 1;
  171.             if oe = noErr then begin
  172.                with pbdt do begin
  173.                   fs.name := '';
  174.                   ioNamePtr := @fs.name;
  175.                   ioVRefNum := fs.vRefNum;
  176.                   oe := PBDTGetPath(@pbdt);
  177.                   if oe = noErr then begin
  178.                      ioIndex := 0;
  179.                      ioFileCreator := creator;
  180.                      oe := PBDTGetAPPLSync(@pbdt);
  181.                      if oe = noErr then
  182.                         found := true;
  183.                   end;
  184.                end;
  185.                oe := noErr;
  186.             end;
  187.          until found or (oe <> noErr);
  188.       end;
  189.       if found then begin
  190.          oe := noErr;
  191.          fs.parID := pbdt.ioAPPLParID;
  192.       end
  193.       else begin
  194.          oe := afpItemNotFound;
  195.          fs.vRefNum := 0;
  196.          fs.parID := 2;
  197.          fs.name := '';
  198.       end;
  199.       FindApplication := oe;
  200.    end;
  201.  
  202.  
  203.  
  204.  
  205.    function FindProcess (creator, typ: OSType; var process: ProcessSerialNumber; var fs: FSSpec): boolean;
  206.       var
  207.          info: ProcessInfoRec;
  208.          oe: OSErr;
  209.          gv: longInt;
  210.    begin
  211.       FindProcess := false;
  212.       if (Gestalt(gestaltOSAttr, gv) = noErr) & (BTST(gv, gestaltLaunchControl)) then begin
  213.          process.highLongOfPSN := 0;
  214.          process.lowLongOfPSN := kNoProcess;
  215.          info.processInfoLength := sizeof(ProcessInfoRec);
  216.          info.processName := nil;
  217.          info.processAppSpec := @fs;
  218.          while GetNextProcess(process) = noErr do begin
  219.             if GetProcessInformation(process, info) = noErr then begin
  220.                if (info.processType = longInt(typ)) and (info.processSignature = creator) then begin
  221.                   FindProcess := true;
  222.                   leave;
  223.                end;
  224.             end;
  225.          end;
  226.       end;
  227.    end;
  228.  
  229.  
  230.  
  231.  
  232.    procedure PrepareToLaunch (var theEvent: AppleEvent; tofront: boolean; var launchDesc: AEDesc; var launchThis: LaunchParamBlockRec);
  233.       var
  234.          oe: OSErr;
  235.    begin
  236.       oe := AECoerceDesc(theEvent, typeAppParameters, launchDesc);
  237.       HLock(handle(theEvent.dataHandle));
  238.       launchThis.launchAppParameters := AppParametersPtr(launchDesc.dataHandle^);
  239.       launchThis.launchBlockID := extendedBlock;
  240.       launchThis.launchEPBLength := extendedBlockLen;
  241.       launchThis.launchFileFlags := 0;
  242.       launchThis.launchControlFlags := launchContinue + launchNoFileFlags;
  243.       if not tofront then
  244.          launchThis.launchControlFlags := launchThis.launchControlFlags + launchDontSwitch;
  245.    end;
  246.  
  247.  
  248.  
  249.  
  250.    procedure LaunchWithDocument (creator, typ: OSType; fs: FSSpec; tofront: boolean);
  251.       var
  252.          psn: ProcessSerialNumber;
  253.          targetAddress: AEDesc;
  254.          theEvent, theReply: AppleEvent;
  255.          fileList: AEDescList;
  256.          launchDesc: AEDesc;
  257.          app_fs: FSSpec;
  258.          launchThis: LaunchParamBlockRec;
  259.          oe: OSErr;
  260.          gv: longInt;
  261.          sendmode: AESendMode;
  262.          t, c: longInt;
  263.    begin
  264.       PurgeSpace(t, c);
  265.       if (Gestalt(gestaltOSAttr, gv) = noErr) & (BTST(gv, gestaltLaunchControl)) & (c > 4096) then begin
  266.          if FindProcess(creator, typ, psn, app_fs) then begin
  267.             oe := AECreateDesc(typeProcessSerialNumber, @psn, sizeof(psn), targetAddress);
  268.             oe := AECreateAppleEvent(kCoreEventClass, kAEOpenDocuments,targetAddress, kAutoGenerateReturnID, kAnyTransactionID, theEvent);
  269.             oe := AEDisposeDesc(targetAddress);
  270.             oe := AECreateList(nil, 0, false, fileList);
  271.             AddFSSToAEList(fileList, 1, fs);
  272.             oe := AEPutParamDesc(theEvent, keyDirectObject, fileList);
  273.             sendmode := kAENoReply;
  274.             if not tofront then
  275.                sendmode := sendmode + kAENeverInteract;
  276.             oe := AESend(theEvent, theReply, sendmode, kAEHighPriority, kNoTimeOut, nil, nil);
  277.             oe := AEDisposeDesc(theEvent);
  278.             oe := AEDisposeDesc(theReply);
  279.             oe := AEDisposeDesc(fileList);
  280.             if tofront then
  281.                oe := SetFrontProcess(psn);
  282.          end
  283.          else begin
  284.             if FindApplication(creator, app_fs) = noErr then begin
  285.                oe := AECreateDesc(typeApplSignature, @creator, sizeof(creator), targetAddress);
  286.                oe := AECreateAppleEvent(kCoreEventClass, kAEOpenDocuments, targetAddress, kAutoGenerateReturnID, kAnyTransactionID, theEvent);
  287.                oe := AEDisposeDesc(targetAddress);
  288.                oe := AECreateList(nil, 0, false, fileList);
  289.                AddFSSToAEList(fileList, 1, fs);
  290.                oe := AEPutParamDesc(theEvent, keyDirectObject, fileList);
  291.                launchThis.launchAppSpec := @app_fs;
  292.                PrepareToLaunch(theEvent, tofront, launchDesc, launchThis);
  293.                oe := LaunchApplication(@launchThis);
  294.                oe := AEDisposeDesc(theEvent);
  295.                oe := AEDisposeDesc(fileList);
  296.             end;
  297.          end;
  298.       end;
  299.    end;
  300.  
  301.  
  302.  
  303.  
  304.    procedure LaunchFSSpec (var fs: FSSpec; tofront: boolean);
  305.       var
  306.          oe: OSErr;
  307.          fi: FInfo;
  308.          targetAddress: AEDesc;
  309.          theEvent: AppleEvent;
  310.          gv: longInt;
  311.          launchThis: LaunchParamBlockRec;
  312.          launchDesc: AEDesc;
  313.    begin
  314.       if (Gestalt(gestaltOSAttr, gv) = noErr) & (BTST(gv, gestaltLaunchControl)) then begin
  315.          oe := FSpGetFInfo(fs, fi);
  316.          oe := AECreateDesc(typeApplSignature, @fi.fdCreator, sizeof(fi.fdCreator), targetAddress);
  317.          oe := AECreateAppleEvent(kCoreEventClass, kAEOpenApplication, targetAddress, kAutoGenerateReturnID, kAnyTransactionID, theEvent);
  318.          oe := AEDisposeDesc(targetAddress);
  319.          launchThis.launchAppSpec := @fs;
  320.          PrepareToLaunch(theEvent, tofront, launchDesc, launchThis);
  321.          oe := LaunchApplication(@launchThis);
  322.          oe := AEDisposeDesc(theEvent);
  323.       end;
  324.    end;
  325.  
  326.  
  327.  
  328.  
  329.    procedure LaunchApp (creator, typ: OSType; tofront: boolean);
  330.       var
  331.          psn: ProcessSerialNumber;
  332.          fileList: AEDescList;
  333.          app_fs: FSSpec;
  334.          oe: OSErr;
  335.          gv: longInt;
  336.          sendmode: AESendMode;
  337.    begin
  338.       if (Gestalt(gestaltOSAttr, gv) = noErr) & (BTST(gv, gestaltLaunchControl)) then begin
  339.          if FindProcess(creator, typ, psn, app_fs) then begin
  340.             if tofront then begin
  341.                oe := SetFrontProcess(psn);
  342.             end;
  343.          end
  344.          else begin
  345.             if FindApplication(creator, app_fs) = noErr then begin
  346.                LaunchFSSpec(app_fs, tofront);
  347.             end;
  348.          end;
  349.       end;
  350.    end;
  351.  
  352.  
  353.  
  354.  
  355.    procedure QuitApplication (creator, typ: OSType);
  356.       var
  357.          process: processSerialNumber;
  358.          infoRec: processInfoRec;
  359.          targetAddress: AEAddressDesc;
  360.          AEvent, AReply: AppleEvent;
  361.          fs: FSSpec;
  362.          oe: OSErr;
  363.    begin
  364.       if FindProcess(creator, typ, process, fs) then begin
  365.          oe := AECreateDesc(typeProcessSerialNumber, @process, SizeOf(process), targetAddress);
  366.          oe := AECreateAppleEvent(kCoreEventClass, kAEQuitApplication, targetAddress, kAutoGenerateReturnID, kAnyTransactionID, AEvent);
  367.          oe := AEDisposeDesc(targetAddress);
  368.          oe := AESend(AEvent, AReply, kAENoReply, kAEHighPriority, 5 * 60, nil, nil);
  369.          oe := AEDisposeDesc(AEvent);
  370.          oe := AEDisposeDesc(AReply);
  371.       end;
  372.    end;
  373.  
  374. end.
  375.  
  376.  
  377.  
  378. -- 
  379. Never use the same Email address twice :-)  No, seriously, my Email 
  380. address is <mailto:peter@kagi.com> for questions about my programs, 
  381. and <mailto:peter@mail.peter.com.au> for any other mail.  They both 
  382. go to the same place currently, so it doesn't make much difference.
  383.